﻿/* 
 *  <copyright file="WorldMapEventMapper.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;
using System.Collections.Generic;
using SobrietyEngine.PlayerData;
using SobrietyEngine.Input;
using SobrietyEngine.User;
using SobrietyEngine.Events;
using SobrietyEngine.Events.Input;
using SobrietyEngine.Guids;
using SobrietyEngine.Input.Mapping;
using SobrietyEngine.Events.UserActions;

using Microsoft.Xna.Framework;

namespace SEGameExample.InputMappers
{
    class WorldMapEventMapper:BaseMapper
    {

        Vector2 moveBy = new Vector2(0, 0);

        public WorldMapEventMapper(IEventRouter eventRouter, CGuid owner)
            : base(eventRouter, owner)
        {
        }

        public override void RegisterEventListeners()
        {

            EventRouter.AddEventListener(typeof(JoystickMove), this);
            EventRouter.AddEventListener(typeof(ButtonPress), this);
            EventRouter.AddEventListener(typeof(ButtonRelease), this);
            EventRouter.AddEventListener(typeof(Trigger), this);
            EventRouter.AddEventListener(typeof(KeyPress), this);
            EventRouter.AddEventListener(typeof(KeyRelease), this);
        }

        public override void UnregisterEventListeners()
        {
            EventRouter.RemoveEventListener(this);
        }

        //joystick / keyboard input events come in, gameServices events go out
        public override void HandleEvent(Event e)
        {
            if (!IsInputMine(e))
                return;

            IInputEvent ie = (IInputEvent)e.Data;
            if (ie.InputDevice.GetType() == typeof(SobrietyEngine.Input.Devices.GamepadDevice))
            {
                if (e.Type == typeof(JoystickMove))
                {
                    JoystickMove jm = (JoystickMove)e.Data;
                    if(jm.Left == true) // left joystick
                        EventRouter.RaiseEvent(this, new Move(Owner, new Vector2(jm.X, jm.Y)), Owner);
                    else
                        EventRouter.RaiseEvent(this, new Rotate(Owner, new Vector2(jm.X, jm.Y)), Owner);

                }
                else if (e.Type == typeof(ButtonPress))
                {
                    ButtonPress bp = (ButtonPress)e.Data;
                    if (bp.Button == GamepadEnums.GamepadButton.X)
                        EventRouter.RaiseEvent(this, new Use(Owner), Owner);

                    bp = (ButtonPress)e.Data;
                    if (bp.Button == GamepadEnums.GamepadButton.A)
                        EventRouter.RaiseEvent(this, new Jump(Owner), Owner);
                }
            }
            else if (ie.InputDevice.GetType() == typeof(SobrietyEngine.Input.Devices.KeyboardDevice))
            {
            }
        }
    }
}
